package com.bulletelc.apollopre.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bulletelc.apollopre.R;

/**
 * Created by CX on 2018/4/2.
 */

public class FoolBar extends RelativeLayout implements View.OnClickListener {
    private ImageButton iBtnLeft;
    private ImageButton iBtnRight;
    private TextView tvTitle;
    private RelativeLayout rlBar;

    private ClickCallback callback;                 // 声明回调函数

    public FoolBar(Context context) {
        this(context, null);
    }

    public FoolBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        View view = LayoutInflater.from(context).inflate(R.layout.foolbar_layout,this, true);
        iBtnLeft = view.findViewById(R.id.iBtnLeft);
        iBtnRight = view.findViewById(R.id.iBtnRight);
        tvTitle = view.findViewById(R.id.tvTitle);
        rlBar = view.findViewById(R.id.rlBar);

        iBtnRight.setOnClickListener(this);
        iBtnLeft.setOnClickListener(this);
    }

    public FoolBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iBtnLeft:
                callback.onLeftClick();
                break;
            case R.id.iBtnRight:
                callback.onRightClick();
                break;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void setDrawableLeft(Drawable res) {
        if (res != null) {
            this.iBtnLeft.setBackground(res);
            return;
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    public void setDrawableRight(Drawable res) {
        if (res != null) {
            this.iBtnRight.setBackground(res);
            return;
        }
    }

    public void setBorderLine(boolean b) {
        if (!b) {
            this.rlBar.setBackgroundColor(getResources().getColor(R.color.white));
        }
    }

    public void setTitle(String title) {
        this.tvTitle.setText(title);
    }

    public void setClickCallback(ClickCallback callback) {
        this.callback = callback;
    }

    /***
     * 导航栏点击回调接口 -- Block中的回调方法
     * 如若需要标题可点击,可再添加
     */
     public interface ClickCallback
    {
        void onLeftClick();
        void onRightClick();
    }
}
